{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MIT-BIH Supraventricular Arrhythmia Database (_svdb_)\n",
    "\n",
    "Part of the ECG Database Collection:\n",
    "\n",
    "| Short Name | Long Name |\n",
    "| :--- | :--- |\n",
    "| _mitdb_ | MIT-BIH Arrhythmia Database |\n",
    "| _svdb_ | MIT-BIH Supraventricular Arrhythmia Database |\n",
    "| _ltdb_ | MIT-BIH Long-Term ECG Database |\n",
    "\n",
    "[Docu](https://wfdb.readthedocs.io/en/latest) of the `wfdb`-package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import wfdb\n",
    "import os\n",
    "from typing import Final\n",
    "from collections.abc import Callable\n",
    "from config import data_raw_folder, data_processed_folder\n",
    "from timeeval import Datasets\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.display import display, Markdown, Latex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking for source datasets in /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database and\n",
      "saving processed datasets in /home/projects/akita/data/benchmark-data/data-processed\n"
     ]
    }
   ],
   "source": [
    "dataset_collection_name = \"SVDB\"\n",
    "source_folder = os.path.join(data_raw_folder, \"MIT-BIH Supraventricular Arrhythmia Database\")\n",
    "target_folder = data_processed_folder\n",
    "\n",
    "from pathlib import Path\n",
    "print(f\"Looking for source datasets in {Path(source_folder).absolute()} and\\nsaving processed datasets in {Path(target_folder).absolute()}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_dataset_names() -> list[str]:\n",
    "    with open(os.path.join(source_folder, \"RECORDS\"), 'r') as f:\n",
    "        records = [l.rstrip('\\n') for l in f]\n",
    "    return records"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def transform_and_label(source_file: str, target: str) -> int:\n",
    "    print(f\"Transforming {os.path.basename(source_file)}\")\n",
    "    # load dataset\n",
    "    record = wfdb.rdrecord(source_file)\n",
    "    df_record = pd.DataFrame(record.p_signal, columns=record.sig_name)\n",
    "    print(f\"  record {record.file_name[0]} loaded\")\n",
    "\n",
    "    # load annotation file\n",
    "    atr = wfdb.rdann(source_file, \"atr\")\n",
    "    assert record.fs == atr.fs, \"Sample frequency of records and annotations does not match!\"\n",
    "    df_annotation = pd.DataFrame(atr.symbol, index=atr.sample, columns=[\"Label\"])\n",
    "    df_annotation = df_annotation.reset_index()\n",
    "    df_annotation.columns = [\"position\", \"label\"]\n",
    "    print(f\"  {atr.ann_len} beat annotations for {source_file} loaded\")\n",
    "\n",
    "    # calculate normal beat length\n",
    "    print(\"  preparing windows for labeling...\")\n",
    "    df_normal_beat = df_annotation.copy()\n",
    "    df_normal_beat[\"prev_position\"] = df_annotation[\"position\"].shift()\n",
    "    df_normal_beat[\"prev_label\"] = df_annotation[\"label\"].shift()\n",
    "    df_normal_beat = df_normal_beat[(df_normal_beat[\"label\"] == \"N\") & (df_normal_beat[\"prev_label\"] == \"N\")]\n",
    "    s_normal_beat_lengths = df_normal_beat[\"position\"] - df_normal_beat[\"prev_position\"]\n",
    "    print(f\"    normal beat distance samples = {len(s_normal_beat_lengths)}\")\n",
    "    normal_beat_length = s_normal_beat_lengths.median()\n",
    "    if (normal_beat_length % 2) == 0:\n",
    "        normal_beat_length += 1\n",
    "    beat_window_size = int(normal_beat_length)\n",
    "    beat_window_margin = (beat_window_size - 1)//2\n",
    "    print(f\"    window size = {beat_window_size}\")\n",
    "    print(f\"    window margins (left and right) = {beat_window_margin}\")\n",
    "\n",
    "    # calculate beat windows\n",
    "    ## for external anomalies\n",
    "    df_ext = df_annotation[(df_annotation[\"label\"] == \"|\") | (df_annotation[\"label\"] == \"Q\")].copy()\n",
    "    df_ext[\"window_start\"] = df_ext[\"position\"]-beat_window_margin\n",
    "    df_ext[\"window_end\"] = df_ext[\"position\"]+beat_window_margin\n",
    "    df_ext = df_ext[[\"position\", \"window_start\", \"window_end\"]]\n",
    "    print(f\"    {len(df_ext)} windows for external anomalies\")\n",
    "    ## for anomalous beats\n",
    "    df_svf = df_annotation[(df_annotation[\"label\"] != \"|\") & (df_annotation[\"label\"] != \"~\") & (df_annotation[\"label\"] != \"+\")].copy()\n",
    "    df_svf[\"position_next\"] = df_svf[\"position\"].shift(-1)\n",
    "    df_svf[\"position_prev\"] = df_svf[\"position\"].shift(1)\n",
    "    #df_svf = df_svf[(df_svf[\"position_prev\"].notnull()) & (df_svf[\"position_next\"].notnull())]\n",
    "    df_svf = df_svf[(df_svf[\"label\"] != \"Q\") & (df_svf[\"label\"] != \"N\")]\n",
    "    df_svf[\"window_start\"] = np.minimum(df_svf[\"position\"].values-beat_window_margin, df_svf[\"position_prev\"].values+beat_window_margin)\n",
    "    df_svf[\"window_end\"] = np.maximum(df_svf[\"position\"].values+beat_window_margin, df_svf[\"position_next\"].values-beat_window_margin)\n",
    "    df_svf = df_svf[[\"position\", \"window_start\", \"window_end\"]]\n",
    "    print(f\"    {len(df_svf)} windows for anomalous beats\")\n",
    "    ## merge\n",
    "    df_windows = pd.concat([df_ext, df_svf])\n",
    "    print(f\"  ...done.\")\n",
    "\n",
    "    # add labels based on anomaly windows\n",
    "    print(\"  labeling\")\n",
    "    df_record[\"is_anomaly\"] = 0\n",
    "    for _, (_, t1, t2) in df_windows.iterrows():\n",
    "        tmp = df_record[df_record.index >= t1]\n",
    "        tmp = tmp[tmp.index <= t2]\n",
    "        df_record[\"is_anomaly\"].values[tmp.index] = 1\n",
    "\n",
    "    # reconstruct timestamps and set as index\n",
    "    print(\"  reconstructing timestamps\")\n",
    "    df_record[\"timestamp\"] = pd.to_datetime(df_record.index.values * 1e+9/record.fs, unit='ns')\n",
    "    df_record = df_record.set_index(\"timestamp\")\n",
    "    df_record.to_csv(target)\n",
    "    print(f\"Dataset {os.path.basename(source_file)} transformed and saved!\")\n",
    "    \n",
    "    # return dataset length\n",
    "    return record.sig_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Directories /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB already exist\n"
     ]
    }
   ],
   "source": [
    "# shared by all datasets\n",
    "dataset_type = \"real\"\n",
    "input_type = \"multivariate\"\n",
    "datetime_index = True\n",
    "train_type = \"unsupervised\"\n",
    "train_is_normal = False\n",
    "\n",
    "# create target directory\n",
    "dataset_subfolder = os.path.join(input_type, dataset_collection_name)\n",
    "target_subfolder = os.path.join(target_folder, dataset_subfolder)\n",
    "try:\n",
    "    os.makedirs(target_subfolder)\n",
    "    print(f\"Created directories {target_subfolder}\")\n",
    "except FileExistsError:\n",
    "    print(f\"Directories {target_subfolder} already exist\")\n",
    "    pass\n",
    "\n",
    "dm = Datasets(target_folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Transforming 800\n",
      "  record 800.dat loaded\n",
      "  1921 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/800 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1772\n",
      "    window size = 119\n",
      "    window margins (left and right) = 59\n",
      "    10 windows for external anomalies\n",
      "    37 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 800 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/800 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/800.test.csv\n",
      "Transforming 801\n",
      "  record 801.dat loaded\n",
      "  2577 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/801 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1830\n",
      "    window size = 97\n",
      "    window margins (left and right) = 48\n",
      "    0 windows for external anomalies\n",
      "    334 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 801 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/801 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/801.test.csv\n",
      "Transforming 802\n",
      "  record 802.dat loaded\n",
      "  1679 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/802 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1555\n",
      "    window size = 135\n",
      "    window margins (left and right) = 67\n",
      "    4 windows for external anomalies\n",
      "    85 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 802 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/802 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/802.test.csv\n",
      "Transforming 803\n",
      "  record 803.dat loaded\n",
      "  2071 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/803 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1776\n",
      "    window size = 113\n",
      "    window margins (left and right) = 56\n",
      "    1 windows for external anomalies\n",
      "    144 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 803 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/803 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/803.test.csv\n",
      "Transforming 804\n",
      "  record 804.dat loaded\n",
      "  2888 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/804 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2451\n",
      "    window size = 83\n",
      "    window margins (left and right) = 41\n",
      "    3 windows for external anomalies\n",
      "    181 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 804 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/804 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/804.test.csv\n",
      "Transforming 805\n",
      "  record 805.dat loaded\n",
      "  2418 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/805 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1882\n",
      "    window size = 111\n",
      "    window margins (left and right) = 55\n",
      "    120 windows for external anomalies\n",
      "    207 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 805 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/805 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/805.test.csv\n",
      "Transforming 806\n",
      "  record 806.dat loaded\n",
      "  3023 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/806 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2890\n",
      "    window size = 77\n",
      "    window margins (left and right) = 38\n",
      "    0 windows for external anomalies\n",
      "    71 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 806 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/806 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/806.test.csv\n",
      "Transforming 807\n",
      "  record 807.dat loaded\n",
      "  1954 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/807 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1881\n",
      "    window size = 121\n",
      "    window margins (left and right) = 60\n",
      "    0 windows for external anomalies\n",
      "    29 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 807 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/807 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/807.test.csv\n",
      "Transforming 808\n",
      "  record 808.dat loaded\n",
      "  1777 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/808 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1732\n",
      "    window size = 131\n",
      "    window margins (left and right) = 65\n",
      "    3 windows for external anomalies\n",
      "    28 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 808 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/808 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/808.test.csv\n",
      "Transforming 809\n",
      "  record 809.dat loaded\n",
      "  2560 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/809 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2418\n",
      "    window size = 93\n",
      "    window margins (left and right) = 46\n",
      "    9 windows for external anomalies\n",
      "    112 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 809 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/809 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/809.test.csv\n",
      "Transforming 810\n",
      "  record 810.dat loaded\n",
      "  1946 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/810 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1777\n",
      "    window size = 123\n",
      "    window margins (left and right) = 61\n",
      "    39 windows for external anomalies\n",
      "    64 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 810 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/810 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/810.test.csv\n",
      "Transforming 811\n",
      "  record 811.dat loaded\n",
      "  1436 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/811 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1392\n",
      "    window size = 163\n",
      "    window margins (left and right) = 81\n",
      "    0 windows for external anomalies\n",
      "    30 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 811 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/811 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/811.test.csv\n",
      "Transforming 812\n",
      "  record 812.dat loaded\n",
      "  1873 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/812 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1664\n",
      "    window size = 125\n",
      "    window margins (left and right) = 62\n",
      "    9 windows for external anomalies\n",
      "    84 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 812 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/812 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/812.test.csv\n",
      "Transforming 820\n",
      "  record 820.dat loaded\n",
      "  2356 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/820 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1971\n",
      "    window size = 99\n",
      "    window margins (left and right) = 49\n",
      "    5 windows for external anomalies\n",
      "    188 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 820 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/820 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/820.test.csv\n",
      "Transforming 821\n",
      "  record 821.dat loaded\n",
      "  3135 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/821 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1880\n",
      "    window size = 75\n",
      "    window margins (left and right) = 37\n",
      "    1 windows for external anomalies\n",
      "    650 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 821 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/821 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/821.test.csv\n",
      "Transforming 822\n",
      "  record 822.dat loaded\n",
      "  2337 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/822 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1017\n",
      "    window size = 97\n",
      "    window margins (left and right) = 48\n",
      "    3 windows for external anomalies\n",
      "    659 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 822 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/822 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/822.test.csv\n",
      "Transforming 823\n",
      "  record 823.dat loaded\n",
      "  2864 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/823 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2133\n",
      "    window size = 85\n",
      "    window margins (left and right) = 42\n",
      "    3 windows for external anomalies\n",
      "    382 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 823 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/823 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/823.test.csv\n",
      "Transforming 824\n",
      "  record 824.dat loaded\n",
      "  2419 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/824 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2016\n",
      "    window size = 97\n",
      "    window margins (left and right) = 48\n",
      "    0 windows for external anomalies\n",
      "    203 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 824 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/824 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/824.test.csv\n",
      "Transforming 825\n",
      "  record 825.dat loaded\n",
      "  2798 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/825 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2507\n",
      "    window size = 83\n",
      "    window margins (left and right) = 41\n",
      "    0 windows for external anomalies\n",
      "    145 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 825 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/825 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/825.test.csv\n",
      "Transforming 826\n",
      "  record 826.dat loaded\n",
      "  2661 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/826 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2508\n",
      "    window size = 89\n",
      "    window margins (left and right) = 44\n",
      "    0 windows for external anomalies\n",
      "    80 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 826 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/826 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/826.test.csv\n",
      "Transforming 827\n",
      "  record 827.dat loaded\n",
      "  1862 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/827 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1826\n",
      "    window size = 125\n",
      "    window margins (left and right) = 62\n",
      "    0 windows for external anomalies\n",
      "    18 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 827 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/827 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/827.test.csv\n",
      "Transforming 828\n",
      "  record 828.dat loaded\n",
      "  1913 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/828 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1602\n",
      "    window size = 123\n",
      "    window margins (left and right) = 61\n",
      "    0 windows for external anomalies\n",
      "    156 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 828 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/828 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/828.test.csv\n",
      "Transforming 829\n",
      "  record 829.dat loaded\n",
      "  1968 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/829 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1806\n",
      "    window size = 119\n",
      "    window margins (left and right) = 59\n",
      "    1 windows for external anomalies\n",
      "    80 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 829 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/829 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/829.test.csv\n",
      "Transforming 840\n",
      "  record 840.dat loaded\n",
      "  2391 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/840 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2330\n",
      "    window size = 97\n",
      "    window margins (left and right) = 48\n",
      "    3 windows for external anomalies\n",
      "    45 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 840 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/840 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/840.test.csv\n",
      "Transforming 841\n",
      "  record 841.dat loaded\n",
      "  1854 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/841 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1138\n",
      "    window size = 131\n",
      "    window margins (left and right) = 65\n",
      "    3 windows for external anomalies\n",
      "    360 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 841 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/841 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/841.test.csv\n",
      "Transforming 842\n",
      "  record 842.dat loaded\n",
      "  2559 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/842 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2259\n",
      "    window size = 95\n",
      "    window margins (left and right) = 47\n",
      "    0 windows for external anomalies\n",
      "    165 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 842 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/842 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/842.test.csv\n",
      "Transforming 843\n",
      "  record 843.dat loaded\n",
      "  2708 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/843 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2607\n",
      "    window size = 85\n",
      "    window margins (left and right) = 42\n",
      "    0 windows for external anomalies\n",
      "    59 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 843 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/843 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/843.test.csv\n",
      "Transforming 844\n",
      "  record 844.dat loaded\n",
      "  1709 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/844 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1607\n",
      "    window size = 139\n",
      "    window margins (left and right) = 69\n",
      "    0 windows for external anomalies\n",
      "    59 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 844 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/844 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/844.test.csv\n",
      "Transforming 845\n",
      "  record 845.dat loaded\n",
      "  2887 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/845 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2777\n",
      "    window size = 81\n",
      "    window margins (left and right) = 40\n",
      "    2 windows for external anomalies\n",
      "    57 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 845 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/845 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/845.test.csv\n",
      "Transforming 846\n",
      "  record 846.dat loaded\n",
      "  1685 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/846 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1652\n",
      "    window size = 139\n",
      "    window margins (left and right) = 69\n",
      "    2 windows for external anomalies\n",
      "    20 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 846 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/846 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/846.test.csv\n",
      "Transforming 847\n",
      "  record 847.dat loaded\n",
      "  1836 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/847 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1532\n",
      "    window size = 143\n",
      "    window margins (left and right) = 71\n",
      "    41 windows for external anomalies\n",
      "    95 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 847 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/847 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/847.test.csv\n",
      "Transforming 848\n",
      "  record 848.dat loaded\n",
      "  4289 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/848 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 4166\n",
      "    window size = 57\n",
      "    window margins (left and right) = 28\n",
      "    4 windows for external anomalies\n",
      "    31 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 848 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/848 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/848.test.csv\n",
      "Transforming 849\n",
      "  record 849.dat loaded\n",
      "  2161 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/849 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2002\n",
      "    window size = 115\n",
      "    window margins (left and right) = 57\n",
      "    21 windows for external anomalies\n",
      "    80 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 849 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/849 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/849.test.csv\n",
      "Transforming 850\n",
      "  record 850.dat loaded\n",
      "  1840 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/850 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1826\n",
      "    window size = 125\n",
      "    window margins (left and right) = 62\n",
      "    0 windows for external anomalies\n",
      "    8 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 850 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/850 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/850.test.csv\n",
      "Transforming 851\n",
      "  record 851.dat loaded\n",
      "  2769 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/851 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1735\n",
      "    window size = 89\n",
      "    window margins (left and right) = 44\n",
      "    131 windows for external anomalies\n",
      "    459 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 851 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/851 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/851.test.csv\n",
      "Transforming 852\n",
      "  record 852.dat loaded\n",
      "  2665 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/852 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2033\n",
      "    window size = 87\n",
      "    window margins (left and right) = 43\n",
      "    0 windows for external anomalies\n",
      "    330 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 852 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/852 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/852.test.csv\n",
      "Transforming 853\n",
      "  record 853.dat loaded\n",
      "  2245 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/853 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1989\n",
      "    window size = 105\n",
      "    window margins (left and right) = 52\n",
      "    26 windows for external anomalies\n",
      "    112 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 853 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/853 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/853.test.csv\n",
      "Transforming 854\n",
      "  record 854.dat loaded\n",
      "  3109 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/854 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 964\n",
      "    window size = 89\n",
      "    window margins (left and right) = 44\n",
      "    289 windows for external anomalies\n",
      "    938 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 854 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/854 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/854.test.csv\n",
      "Transforming 855\n",
      "  record 855.dat loaded\n",
      "  2577 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/855 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1584\n",
      "    window size = 93\n",
      "    window margins (left and right) = 46\n",
      "    7 windows for external anomalies\n",
      "    504 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 855 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/855 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/855.test.csv\n",
      "Transforming 856\n",
      "  record 856.dat loaded\n",
      "  2876 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/856 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2761\n",
      "    window size = 81\n",
      "    window margins (left and right) = 40\n",
      "    9 windows for external anomalies\n",
      "    40 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 856 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/856 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/856.test.csv\n",
      "Transforming 857\n",
      "  record 857.dat loaded\n",
      "  2610 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/857 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2234\n",
      "    window size = 93\n",
      "    window margins (left and right) = 46\n",
      "    4 windows for external anomalies\n",
      "    182 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 857 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/857 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/857.test.csv\n",
      "Transforming 858\n",
      "  record 858.dat loaded\n",
      "  2187 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/858 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2160\n",
      "    window size = 107\n",
      "    window margins (left and right) = 53\n",
      "    5 windows for external anomalies\n",
      "    11 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 858 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/858 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/858.test.csv\n",
      "Transforming 859\n",
      "  record 859.dat loaded\n",
      "  3557 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/859 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2547\n",
      "    window size = 65\n",
      "    window margins (left and right) = 32\n",
      "    1 windows for external anomalies\n",
      "    533 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 859 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/859 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/859.test.csv\n",
      "Transforming 860\n",
      "  record 860.dat loaded\n",
      "  2463 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/860 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 941\n",
      "    window size = 97\n",
      "    window margins (left and right) = 48\n",
      "    25 windows for external anomalies\n",
      "    744 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 860 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/860 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/860.test.csv\n",
      "Transforming 861\n",
      "  record 861.dat loaded\n",
      "  2582 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/861 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1292\n",
      "    window size = 95\n",
      "    window margins (left and right) = 47\n",
      "    55 windows for external anomalies\n",
      "    596 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 861 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/861 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/861.test.csv\n",
      "Transforming 862\n",
      "  record 862.dat loaded\n",
      "  2597 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/862 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1707\n",
      "    window size = 101\n",
      "    window margins (left and right) = 50\n",
      "    398 windows for external anomalies\n",
      "    49 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 862 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/862 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/862.test.csv\n",
      "Transforming 863\n",
      "  record 863.dat loaded\n",
      "  3129 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/863 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2027\n",
      "    window size = 75\n",
      "    window margins (left and right) = 37\n",
      "    2 windows for external anomalies\n",
      "    562 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 863 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/863 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/863.test.csv\n",
      "Transforming 864\n",
      "  record 864.dat loaded\n",
      "  1907 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/864 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1652\n",
      "    window size = 125\n",
      "    window margins (left and right) = 62\n",
      "    0 windows for external anomalies\n",
      "    151 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 864 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/864 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/864.test.csv\n",
      "Transforming 865\n",
      "  record 865.dat loaded\n",
      "  3701 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/865 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 759\n",
      "    window size = 99\n",
      "    window margins (left and right) = 49\n",
      "    515 windows for external anomalies\n",
      "    2053 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 865 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/865 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/865.test.csv\n",
      "Transforming 866\n",
      "  record 866.dat loaded\n",
      "  2666 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/866 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1533\n",
      "    window size = 103\n",
      "    window margins (left and right) = 51\n",
      "    0 windows for external anomalies\n",
      "    614 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 866 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/866 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/866.test.csv\n",
      "Transforming 867\n",
      "  record 867.dat loaded\n",
      "  3000 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/867 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2641\n",
      "    window size = 63\n",
      "    window margins (left and right) = 31\n",
      "    0 windows for external anomalies\n",
      "    211 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 867 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/867 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/867.test.csv\n",
      "Transforming 868\n",
      "  record 868.dat loaded\n",
      "  3445 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/868 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1821\n",
      "    window size = 71\n",
      "    window margins (left and right) = 35\n",
      "    17 windows for external anomalies\n",
      "    783 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 868 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/868 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/868.test.csv\n",
      "Transforming 869\n",
      "  record 869.dat loaded\n",
      "  2163 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/869 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 970\n",
      "    window size = 119\n",
      "    window margins (left and right) = 59\n",
      "    0 windows for external anomalies\n",
      "    654 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 869 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/869 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/869.test.csv\n",
      "Transforming 870\n",
      "  record 870.dat loaded\n",
      "  2791 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/870 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 970\n",
      "    window size = 99\n",
      "    window margins (left and right) = 49\n",
      "    80 windows for external anomalies\n",
      "    892 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 870 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/870 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/870.test.csv\n",
      "Transforming 871\n",
      "  record 871.dat loaded\n",
      "  1803 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/871 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1750\n",
      "    window size = 125\n",
      "    window margins (left and right) = 62\n",
      "    3 windows for external anomalies\n",
      "    20 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 871 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/871 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/871.test.csv\n",
      "Transforming 872\n",
      "  record 872.dat loaded\n",
      "  1981 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/872 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1814\n",
      "    window size = 117\n",
      "    window margins (left and right) = 58\n",
      "    0 windows for external anomalies\n",
      "    83 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 872 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/872 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/872.test.csv\n",
      "Transforming 873\n",
      "  record 873.dat loaded\n",
      "  1684 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/873 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1568\n",
      "    window size = 137\n",
      "    window margins (left and right) = 68\n",
      "    0 windows for external anomalies\n",
      "    58 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 873 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/873 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/873.test.csv\n",
      "Transforming 874\n",
      "  record 874.dat loaded\n",
      "  2240 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/874 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2146\n",
      "    window size = 103\n",
      "    window margins (left and right) = 51\n",
      "    0 windows for external anomalies\n",
      "    50 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 874 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/874 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/874.test.csv\n",
      "Transforming 875\n",
      "  record 875.dat loaded\n",
      "  2052 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/875 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1847\n",
      "    window size = 113\n",
      "    window margins (left and right) = 56\n",
      "    0 windows for external anomalies\n",
      "    106 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 875 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/875 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/875.test.csv\n",
      "Transforming 876\n",
      "  record 876.dat loaded\n",
      "  2142 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/876 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1871\n",
      "    window size = 111\n",
      "    window margins (left and right) = 55\n",
      "    1 windows for external anomalies\n",
      "    135 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 876 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/876 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/876.test.csv\n",
      "Transforming 877\n",
      "  record 877.dat loaded\n",
      "  2038 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/877 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1652\n",
      "    window size = 117\n",
      "    window margins (left and right) = 58\n",
      "    0 windows for external anomalies\n",
      "    197 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 877 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/877 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/877.test.csv\n",
      "Transforming 878\n",
      "  record 878.dat loaded\n",
      "  1927 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/878 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1270\n",
      "    window size = 121\n",
      "    window margins (left and right) = 60\n",
      "    6 windows for external anomalies\n",
      "    320 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 878 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/878 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/878.test.csv\n",
      "Transforming 879\n",
      "  record 879.dat loaded\n",
      "  2089 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/879 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 867\n",
      "    window size = 113\n",
      "    window margins (left and right) = 56\n",
      "    7 windows for external anomalies\n",
      "    590 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 879 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/879 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/879.test.csv\n",
      "Transforming 880\n",
      "  record 880.dat loaded\n",
      "  3487 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/880 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2731\n",
      "    window size = 67\n",
      "    window margins (left and right) = 33\n",
      "    0 windows for external anomalies\n",
      "    382 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 880 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/880 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/880.test.csv\n",
      "Transforming 881\n",
      "  record 881.dat loaded\n",
      "  2302 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/881 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 490\n",
      "    window size = 105\n",
      "    window margins (left and right) = 52\n",
      "    13 windows for external anomalies\n",
      "    936 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 881 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/881 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/881.test.csv\n",
      "Transforming 882\n",
      "  record 882.dat loaded\n",
      "  1935 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/882 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1843\n",
      "    window size = 119\n",
      "    window margins (left and right) = 59\n",
      "    0 windows for external anomalies\n",
      "    46 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 882 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/882 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/882.test.csv\n",
      "Transforming 883\n",
      "  record 883.dat loaded\n",
      "  1822 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/883 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1729\n",
      "    window size = 127\n",
      "    window margins (left and right) = 63\n",
      "    2 windows for external anomalies\n",
      "    47 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 883 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/883 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/883.test.csv\n",
      "Transforming 884\n",
      "  record 884.dat loaded\n",
      "  3022 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/884 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2098\n",
      "    window size = 81\n",
      "    window margins (left and right) = 40\n",
      "    126 windows for external anomalies\n",
      "    397 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 884 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/884 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/884.test.csv\n",
      "Transforming 885\n",
      "  record 885.dat loaded\n",
      "  2054 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/885 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1024\n",
      "    window size = 123\n",
      "    window margins (left and right) = 61\n",
      "    38 windows for external anomalies\n",
      "    453 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 885 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/885 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/885.test.csv\n",
      "Transforming 886\n",
      "  record 886.dat loaded\n",
      "  2223 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/886 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2115\n",
      "    window size = 105\n",
      "    window margins (left and right) = 52\n",
      "    0 windows for external anomalies\n",
      "    54 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 886 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/886 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/886.test.csv\n",
      "Transforming 887\n",
      "  record 887.dat loaded\n",
      "  2822 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/887 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1921\n",
      "    window size = 83\n",
      "    window margins (left and right) = 41\n",
      "    23 windows for external anomalies\n",
      "    335 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 887 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/887 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/887.test.csv\n",
      "Transforming 888\n",
      "  record 888.dat loaded\n",
      "  2350 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/888 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2053\n",
      "    window size = 101\n",
      "    window margins (left and right) = 50\n",
      "    21 windows for external anomalies\n",
      "    122 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 888 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/888 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/888.test.csv\n",
      "Transforming 889\n",
      "  record 889.dat loaded\n",
      "  1738 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/889 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1210\n",
      "    window size = 135\n",
      "    window margins (left and right) = 67\n",
      "    5 windows for external anomalies\n",
      "    243 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 889 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/889 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/889.test.csv\n",
      "Transforming 890\n",
      "  record 890.dat loaded\n",
      "  2174 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/890 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1835\n",
      "    window size = 107\n",
      "    window margins (left and right) = 53\n",
      "    0 windows for external anomalies\n",
      "    171 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 890 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/890 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/890.test.csv\n",
      "Transforming 891\n",
      "  record 891.dat loaded\n",
      "  2779 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/891 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1630\n",
      "    window size = 89\n",
      "    window margins (left and right) = 44\n",
      "    165 windows for external anomalies\n",
      "    484 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 891 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/891 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/891.test.csv\n",
      "Transforming 892\n",
      "  record 892.dat loaded\n",
      "  2857 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/892 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 764\n",
      "    window size = 87\n",
      "    window margins (left and right) = 43\n",
      "    7 windows for external anomalies\n",
      "    1100 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 892 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/892 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/892.test.csv\n",
      "Transforming 893\n",
      "  record 893.dat loaded\n",
      "  2528 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/893 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 1860\n",
      "    window size = 93\n",
      "    window margins (left and right) = 46\n",
      "    0 windows for external anomalies\n",
      "    339 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 893 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/893 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/893.test.csv\n",
      "Transforming 894\n",
      "  record 894.dat loaded\n",
      "  2434 beat annotations for /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/894 loaded\n",
      "  preparing windows for labeling...\n",
      "    normal beat distance samples = 2018\n",
      "    window size = 99\n",
      "    window margins (left and right) = 49\n",
      "    22 windows for external anomalies\n",
      "    133 windows for anomalous beats\n",
      "  ...done.\n",
      "  labeling\n",
      "  reconstructing timestamps\n",
      "Dataset 894 transformed and saved!\n",
      "Processed source dataset /home/projects/akita/data/benchmark-data/data-raw/MIT-BIH Supraventricular Arrhythmia Database/894 -> /home/projects/akita/data/benchmark-data/data-processed/multivariate/SVDB/894.test.csv\n"
     ]
    }
   ],
   "source": [
    "# dataset transformation\n",
    "transform_file: Callable[[str, str], int] = transform_and_label\n",
    "\n",
    "for dataset_name in load_dataset_names():\n",
    "    # intentionally no file suffix (.dat)\n",
    "    source_file = os.path.join(source_folder, dataset_name)\n",
    "    filename = f\"{dataset_name}.test.csv\"\n",
    "    path = os.path.join(dataset_subfolder, filename)\n",
    "    target_filepath = os.path.join(target_subfolder, filename)\n",
    "            \n",
    "    # transform file and label it\n",
    "    dataset_length = transform_file(source_file, target_filepath)\n",
    "    print(f\"Processed source dataset {source_file} -> {target_filepath}\")\n",
    "\n",
    "    # save metadata\n",
    "    dm.add_dataset((dataset_collection_name, dataset_name),\n",
    "        train_path = None,\n",
    "        test_path = path,\n",
    "        dataset_type = dataset_type,\n",
    "        datetime_index = datetime_index,\n",
    "        split_at = None,\n",
    "        train_type = train_type,\n",
    "        train_is_normal = train_is_normal,\n",
    "        input_type = input_type,\n",
    "        dataset_length = dataset_length\n",
    "    )\n",
    "\n",
    "# save metadata of benchmark\n",
    "dm.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>train_path</th>\n",
       "      <th>test_path</th>\n",
       "      <th>dataset_type</th>\n",
       "      <th>datetime_index</th>\n",
       "      <th>split_at</th>\n",
       "      <th>train_type</th>\n",
       "      <th>train_is_normal</th>\n",
       "      <th>input_type</th>\n",
       "      <th>length</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>collection_name</th>\n",
       "      <th>dataset_name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"11\" valign=\"top\">SVDB</th>\n",
       "      <th>800</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/SVDB/800.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>230400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>801</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/SVDB/801.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>230400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>802</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/SVDB/802.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>230400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>803</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/SVDB/803.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>230400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>804</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/SVDB/804.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>230400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>890</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/SVDB/890.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>230400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/SVDB/891.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>230400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>892</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/SVDB/892.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>230400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>893</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/SVDB/893.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>230400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>894</th>\n",
       "      <td>NaN</td>\n",
       "      <td>multivariate/SVDB/894.test.csv</td>\n",
       "      <td>real</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>unsupervised</td>\n",
       "      <td>False</td>\n",
       "      <td>multivariate</td>\n",
       "      <td>230400</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>78 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                             train_path                       test_path  \\\n",
       "collection_name dataset_name                                              \n",
       "SVDB            800                 NaN  multivariate/SVDB/800.test.csv   \n",
       "                801                 NaN  multivariate/SVDB/801.test.csv   \n",
       "                802                 NaN  multivariate/SVDB/802.test.csv   \n",
       "                803                 NaN  multivariate/SVDB/803.test.csv   \n",
       "                804                 NaN  multivariate/SVDB/804.test.csv   \n",
       "...                                 ...                             ...   \n",
       "                890                 NaN  multivariate/SVDB/890.test.csv   \n",
       "                891                 NaN  multivariate/SVDB/891.test.csv   \n",
       "                892                 NaN  multivariate/SVDB/892.test.csv   \n",
       "                893                 NaN  multivariate/SVDB/893.test.csv   \n",
       "                894                 NaN  multivariate/SVDB/894.test.csv   \n",
       "\n",
       "                             dataset_type  datetime_index  split_at  \\\n",
       "collection_name dataset_name                                          \n",
       "SVDB            800                  real            True       NaN   \n",
       "                801                  real            True       NaN   \n",
       "                802                  real            True       NaN   \n",
       "                803                  real            True       NaN   \n",
       "                804                  real            True       NaN   \n",
       "...                                   ...             ...       ...   \n",
       "                890                  real            True       NaN   \n",
       "                891                  real            True       NaN   \n",
       "                892                  real            True       NaN   \n",
       "                893                  real            True       NaN   \n",
       "                894                  real            True       NaN   \n",
       "\n",
       "                                train_type  train_is_normal    input_type  \\\n",
       "collection_name dataset_name                                                \n",
       "SVDB            800           unsupervised            False  multivariate   \n",
       "                801           unsupervised            False  multivariate   \n",
       "                802           unsupervised            False  multivariate   \n",
       "                803           unsupervised            False  multivariate   \n",
       "                804           unsupervised            False  multivariate   \n",
       "...                                    ...              ...           ...   \n",
       "                890           unsupervised            False  multivariate   \n",
       "                891           unsupervised            False  multivariate   \n",
       "                892           unsupervised            False  multivariate   \n",
       "                893           unsupervised            False  multivariate   \n",
       "                894           unsupervised            False  multivariate   \n",
       "\n",
       "                              length  \n",
       "collection_name dataset_name          \n",
       "SVDB            800           230400  \n",
       "                801           230400  \n",
       "                802           230400  \n",
       "                803           230400  \n",
       "                804           230400  \n",
       "...                              ...  \n",
       "                890           230400  \n",
       "                891           230400  \n",
       "                892           230400  \n",
       "                893           230400  \n",
       "                894           230400  \n",
       "\n",
       "[78 rows x 9 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dm.refresh()\n",
    "dm.df().loc[(slice(dataset_collection_name,dataset_collection_name), slice(None))]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dataset transformation walk-through"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_obj_attr(obj, name=\"Object\"):\n",
    "    print(name)\n",
    "    tmp = vars(obj)\n",
    "    for key in tmp:\n",
    "        print(key, tmp[key])\n",
    "    print(\"\")\n",
    "records = load_dataset_names()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load and parse dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dataset\n",
    "record = wfdb.rdrecord(os.path.join(source_folder, records[51]))\n",
    "#print_obj_attr(record, \"Record object\")\n",
    "\n",
    "df_record = pd.DataFrame(record.p_signal, columns=record.sig_name)\n",
    "df_record"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add timestamp information based on sample interval ($$[fs] = samples/second$$):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "display(Latex(f\"Samples per second: $$fs = {record.fs} \\\\frac{{1}}{{s}}$$\"))\n",
    "display(Markdown(f\"This gives a sample interval of {1e+9/record.fs} nanoseconds\"))\n",
    "df_record[\"timestamp\"] = pd.to_datetime(df_record.index.values * 1e+9/record.fs, unit='ns')\n",
    "df_record"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# find all annotations\n",
    "records = load_dataset_names()\n",
    "annotations = {}\n",
    "for r in records:\n",
    "    atr = wfdb.rdann(os.path.join(source_folder, r), \"atr\")\n",
    "    df_annotation = pd.DataFrame(atr.symbol, index=atr.sample, columns=[\"Label\"])\n",
    "    for an in df_annotation[\"Label\"].unique():\n",
    "        if an not in annotations:\n",
    "            annotations[an] = set()\n",
    "        annotations[an].add(atr.record_name)\n",
    "\n",
    "for an in annotations:\n",
    "    annotations[an] = \", \".join(annotations[an])\n",
    "annotations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Annotations\n",
    "\n",
    "| Annotation | Description |\n",
    "| :--------- | :---------- |\n",
    "|| **Considered normal** |\n",
    "| `N` | Normal beat |\n",
    "|| **Anomalous beats** (use double-window labeling) |\n",
    "| `F` | Fusion of ventricular and normal beat |\n",
    "| `S` | Supraventricular premature or ectopic beat |\n",
    "| `a` | Aberrated atrial premature beat |\n",
    "| `V` | Premature ventricular contraction |\n",
    "| `J` | Nodal (junctional) premature beat |\n",
    "| `B` | Bundle branch block beat (unspecified) |\n",
    "|| **External anomalies** (single window labeling) |\n",
    "| `Q` | Unclassifiable beat |\n",
    "| `\\|` | Isolated QRS-like artifact |\n",
    "|| **Ignored, bc hard to parse and to label** |\n",
    "| `+` | Rythm change |\n",
    "| `~` | Change in signal quality (usually noise level changes) |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load and parse annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atr = wfdb.rdann(os.path.join(source_folder, records[51]), \"atr\")\n",
    "#print_obj_attr(atr, \"Annotation object\")\n",
    "assert record.fs == atr.fs, \"Sample frequency of records and annotations does not match!\"\n",
    "\n",
    "df_annotation = pd.DataFrame(atr.symbol, index=atr.sample, columns=[\"Label\"])\n",
    "df_annotation = df_annotation.reset_index()\n",
    "df_annotation.columns = [\"position\", \"label\"]\n",
    "df_annotation.groupby(\"label\").count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculate beat window\n",
    "\n",
    "We assume that the normal beats (annotated with `N`) occur in a regular interval and that the expert annotations (from the dataset) are directly in the middle of a beat window.\n",
    "A beat window is a fixed length subsequence of the time series and shows a heart beat in its direct (local) context.\n",
    "\n",
    "We calculate the beat window length for each dataset based on the median distance between normal beats (`N`).\n",
    "The index (autoincrementing integers) serves as the measurement unit.\n",
    "\n",
    "Create DataFrame containing all annotated beats:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_beat = df_annotation[[\"position\", \"label\"]]\n",
    "df_beat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Shifted-by-one self-join and filter out all beat-pairs that contain anomalous beats.\n",
    "We want to calculate the beat windows only based on the normal beats.\n",
    "We then calculate the distance between two neighboring heart beats:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_normal_beat = df_beat.copy()\n",
    "df_normal_beat[\"prev_position\"] = df_beat[\"position\"].shift()\n",
    "df_normal_beat[\"prev_label\"] = df_beat[\"label\"].shift()\n",
    "df_normal_beat = df_normal_beat[(df_normal_beat[\"label\"] == \"N\") & (df_normal_beat[\"prev_label\"] == \"N\")]\n",
    "df_normal_beat = df_normal_beat.drop(columns=[\"label\", \"prev_label\"])\n",
    "df_normal_beat[\"length\"] = df_normal_beat[\"position\"] - df_normal_beat[\"prev_position\"]\n",
    "df_normal_beat.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The median of all normal beat lengths is the beat window size.\n",
    "We require the beat window size to be odd.\n",
    "This allows us to center the window at the beat annotation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "normal_beat_length = df_normal_beat[\"length\"].median()\n",
    "if (normal_beat_length%2) == 0:\n",
    "    normal_beat_length += 1\n",
    "beat_window_size = int(normal_beat_length)\n",
    "beat_window_margin = (beat_window_size - 1)//2\n",
    "print(f\"window size = {beat_window_size}\\nwindow margins (left and right) = {beat_window_margin}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculate anomalous windows\n",
    "\n",
    "The experts from PhysioNet annotated only the beats itself with a label, but the actual anomaly is also comprised of the beat surroundings.\n",
    "\n",
    "We assume that anomalous beats (such as `V` or `F`; see table above) require looking at a window around the actual beat as being anomalous.\n",
    "External anomalies (such as `|`; see table above) also mark a window around it as anomalous, because those artefacts comprise multiple points.\n",
    "\n",
    "We completely ignore `~` and `+`-annotations that indicate signal quality or rythm changes, because they are not relevant for our analysis.\n",
    "\n",
    "We automatically label a variable-sized window around an annotated beat as an anomalous subsequence using the following technique:\n",
    "\n",
    "1. For anomalous annotations (`S`, `V`, `a`, `J`, `B`, and `F` annotations):\n",
    "   - Remove `~`, `+`, and `|` annotations\n",
    "   - Calculate anomaly window using `beat_window_size` aligned with its center on the beat annotation.\n",
    "   - Calculate end of previous beat window _e_ and beginning of next beat window _b_.\n",
    "     Use _e_ as beginning and _b_ as end for a second anomaly window.\n",
    "   - Mark the union of both anomaly windows' points as anomalous.\n",
    "2. For `|` and `Q` annotations, mark all points of an anomaly window centered on the annotation as anomalous.\n",
    "3. Mark all other points as normal.\n",
    "\n",
    "> **Explain, why we used the combined windows for anomalous beats!!**\n",
    ">\n",
    "> - pattern/shape of signal may be ok\n",
    "> - but we consider distance to other beats also\n",
    "> - if too narrow or too far away, it's also anomalous\n",
    "\n",
    "The figure shows an anomalous beat with its anomaly window (in red) and the windows of its previous and subsequent normal beats (in green).\n",
    "We mark all points in the interval $$[min(W_{end}, X_{start}), max(X_{end}, Y_{start})]$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# reverse lookup from timestamp to annotation index in df_beat\n",
    "p = df_record[df_record[\"timestamp\"] == \"1970-01-01 00:11:03.000\"].index.values[0]\n",
    "df_beat[df_beat[\"position\"] >= p].index[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_window(pos, color=\"blue\", **kvs):\n",
    "    start = pos - beat_window_margin\n",
    "    end = pos + beat_window_margin\n",
    "    plt.axvspan(start, end, color=color, alpha=0.5, **kvs)\n",
    "\n",
    "\n",
    "index = 798\n",
    "\n",
    "beat_n = df_beat.loc[index, \"position\"]\n",
    "print(\"Selected beat is annotated as\", df_beat.loc[index, \"label\"])\n",
    "print(\"with timestamp\", df_record.loc[beat_n, \"timestamp\"])\n",
    "ax = df_record.iloc[beat_n-500:beat_n+500].plot(kind='line', y=['ECG1', 'ECG2'], use_index=True, figsize=(20,10))\n",
    "plot_window(df_beat.loc[index-1, \"position\"], label=\"$W$\")\n",
    "plot_window(beat_n, color=\"orange\", label=\"$X$\")\n",
    "plot_window(df_beat.loc[index+1, \"position\"], label=\"$Y$\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Windows for external anomalies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_pipe = df_beat.copy()\n",
    "df_pipe = df_pipe[(df_pipe[\"label\"] == \"|\") | (df_pipe[\"label\"] == \"Q\")]\n",
    "df_pipe[\"window_start\"] = df_pipe[\"position\"]-beat_window_margin\n",
    "df_pipe[\"window_end\"] = df_pipe[\"position\"]+beat_window_margin\n",
    "df_pipe = df_pipe[[\"position\", \"window_start\", \"window_end\"]]\n",
    "df_pipe.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Windows for anomalous beats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_tmp = df_beat.copy()\n",
    "df_tmp = df_tmp[(df_tmp[\"label\"] != \"|\") & (df_tmp[\"label\"] != \"~\") & (df_tmp[\"label\"] != \"+\")]\n",
    "df_tmp[\"position_next\"] = df_tmp[\"position\"].shift(-1)\n",
    "df_tmp[\"position_prev\"] = df_tmp[\"position\"].shift(1)\n",
    "#df_tmp = df_tmp[(df_tmp[\"position_prev\"].notnull()) & (df_tmp[\"position_next\"].notnull())]\n",
    "df_tmp = df_tmp[(df_tmp[\"label\"] != \"Q\") & (df_tmp[\"label\"] != \"N\")]\n",
    "df_tmp[\"window_start\"] = np.minimum(df_tmp[\"position\"].values-beat_window_margin, df_tmp[\"position_prev\"].values+beat_window_margin)\n",
    "df_tmp[\"window_end\"] = np.maximum(df_tmp[\"position\"].values+beat_window_margin, df_tmp[\"position_next\"].values-beat_window_margin)\n",
    "df_svf = df_tmp[[\"position\", \"window_start\", \"window_end\"]]\n",
    "df_tmp.groupby(\"label\").count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Merge everything together"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_windows = pd.concat([df_pipe, df_svf])\n",
    "df_windows.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index = 798\n",
    "\n",
    "beat = df_windows.loc[index, \"position\"]\n",
    "start = df_windows.loc[index, \"window_start\"]\n",
    "end = df_windows.loc[index, \"window_end\"]\n",
    "print(\"Selected beat is annotated as\", df_beat.loc[index, \"label\"])\n",
    "print(\"with timestamp\", df_record.loc[beat, \"timestamp\"])\n",
    "ax = df_record.iloc[beat-500:beat+500].plot(kind='line', y=['ECG1', 'ECG2'], use_index=True, figsize=(20,10))\n",
    "plt.axvspan(beat-500, start-1, color=\"green\", alpha=0.5, label=\"normal region 1\", ymin=.5)\n",
    "plt.axvspan(start, end, color=\"red\", alpha=0.5, label=\"anomalous region\", ymin=.5)\n",
    "plt.axvspan(end+1, beat+500, color=\"green\", alpha=0.5, label=\"normal region 2\", ymin=.5)\n",
    "plot_window(df_beat.loc[index-1, \"position\"], label=\"$W$\", ymax=.5)\n",
    "plot_window(beat_n, color=\"orange\", label=\"$X$\", ymax=.5)\n",
    "plot_window(df_beat.loc[index+1, \"position\"], label=\"$Y$\", ymax=.5)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df_record.copy()\n",
    "df[\"is_anomaly\"] = 0\n",
    "\n",
    "for _, (_, t1, t2) in df_windows.iterrows():\n",
    "    tmp = df[df.index >= t1]\n",
    "    tmp = tmp[tmp.index <= t2]\n",
    "    df[\"is_anomaly\"].values[tmp.index] = 1\n",
    "\n",
    "#df = df.set_index(\"timestamp\")\n",
    "df[df[\"is_anomaly\"] == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_beat[(df_beat[\"label\"] == \"|\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start = 21700\n",
    "end = 22500\n",
    "df_show = df.loc[start:end]\n",
    "df_show.plot(kind='line', y=['ECG1', 'ECG2', 'is_anomaly'], use_index=True, figsize=(20,10))\n",
    "\n",
    "labels = df_beat[(df_beat[\"position\"] > start) & (df_beat[\"position\"] < end)]\n",
    "for i, (position, label) in labels.iterrows():\n",
    "    plt.text(position, -2.5, label)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Experimentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.merge(df_record, df_annotation, left_index=True, right_index=True, how=\"outer\")\n",
    "#df = df.fillna(value={\"Label\": \".\", \"is_anomaly\": 0})\n",
    "df.groupby([\"is_anomaly\"]).count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[df[\"Label\"].notna()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "df_show = df.loc[27000:28000]\n",
    "df_show.plot(kind='line', y=['ECG1', 'ECG2', 'is_anomaly'], use_index=True, figsize=(20,10))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(os.path.join(dataset_subfolder, \"800.test.csv\"), index_col=\"timestamp\")\n",
    "df.loc[\"1970-01-01 00:21:20\":\"1970-01-01 00:21:40\"].plot(figsize=(20,10))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "timeeval",
   "language": "python",
   "name": "timeeval"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
